<template>
	<view :style="colorStyle">
		<view class="navbar acea-row row-around">
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 1 }" @click="onNav(1)">{{$t(`未使用`)}}
			</view>
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 2 }" @click="onNav(2)">
				{{$t(`已使用/过期`)}}
			</view>
		</view>
		<view class='coupon-list' v-if="couponsList.length">
			<view class='item acea-row row-center-wrapper' v-for='(item,index) in couponsList' :key="index"
				:class="{svip: item.receive_type === 4}">
				<view class="moneyCon acea-row row-center-wrapper">
					<view class='money' :class='item._type == 0 ? "moneyGray" : ""'>
						<view>{{$t(`￥`)}}<text class='num'>{{item.coupon_price}}</text></view>
						<view class="pic-num" v-if="item.use_min_price > 0">
							{{$t(`满`)}}{{item.use_min_price}}{{$t(`元可用`)}}
						</view>
						<view class="pic-num" v-else>{{$t(`无门槛券`)}}</view>
					</view>
				</view>
				<view class='text'>
					<view class='condition' v-if="item.receive_type==5&&item.pc_msg=='可使用'" style="height: 44rpx;">
						<view class="name line2" style="width: 100%;">
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-if="item.applicable_type === 0">{{$t(`通用劵`)}}</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-else-if="item.applicable_type === 1">{{$t(`品类券`)}}</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-else>{{$t(`商品券`)}}</view>
							<image src="../../../static/images/fvip.png" class="pic" v-if="item.receive_type===4">
							</image>
							{{$t(item.coupon_title)}}
							<!-- <button class='bnt bg-color zhuanzeng' v-if="item.receive_type===5"  @tap="customShare">转赠</button> -->
						</view>
					</view>
					<view class='condition' v-else>
						<view class="name line2" style="width: 100%;">
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-if="item.applicable_type === 0">{{$t(`通用劵`)}}</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-else-if="item.applicable_type === 1">{{$t(`品类券`)}}</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-else>{{$t(`商品券`)}}</view>
							<image src="../../../static/images/fvip.png" class="pic" v-if="item.receive_type===4">
							</image>
							{{$t(item.coupon_title)}}
							<!-- <button class='bnt bg-color zhuanzeng' v-if="item.receive_type===5"  @tap="customShare">转赠</button> -->
						</view>
					</view>
					<view class='data acea-row row-between-wrapper' @click="useCoupon(item)">
						<view>{{item.add_time}}-{{item.end_time}}</view>
						<view class='bnt gray' v-if="item._type==0">{{$t(item._msg)}}</view>
						<view class='bnt bg-color' v-else>{{$t(item._msg)}}</view>
					</view>
					<!-- <view class="data acea-row row-between-wrapper" > -->
					<button v-if="item.receive_type===5&&item.pc_msg=='可使用'&&item.is_show==0" class='zhuanzeng' @click="zhuanzengFn(item)">
						<image class="zhuan-img" src="../../../static/images/zhuanfa.png" mode=""></image>
						转赠
					</button>
				<!-- 	<button v-if="item.receive_type===5&&item.pc_msg=='可使用'&&item.is_show==0" class='zhuanzeng' @click="appShare(item)">
						<image class="zhuan-img" src="../../../static/images/zhuanfa.png" mode=""></image>
						转赠
					</button> -->
					<button v-if="item.receive_type===5&&item.pc_msg=='可使用'&&item.is_show==1" class='zhuanzeng1'
						:data-rid="item">
						<!-- <image class="zhuan-img" src="../../../static/images/zhuanfa.png" mode=""></image> -->
						已转赠
					</button>
					<!-- </view> -->
				</view>
			</view>
		</view>
		<view class='noCommodity' v-if="!couponsList.length && page === 2">
			<view class='pictrue'>
				<image :src="imgHost + '/statics/images/noCoupon.png'"></image>
			</view>
		</view>
		<view class="mengbic" v-if="zengsong.status=='未使用'&&show==1">

		</view>
		<view class="tanchuang" v-if="zengsong.status=='未使用'&&show==1">
			<view class="tanchuang-top">
				<image src="https://youzhenimg.sdshengyue.cn/attach/2024/01/de227202401181620494758.png" mode=""
					style="width: 100%;height: 194rpx;"></image>
			</view>
			<view class="tanchuang-center">
				<view class="top-jia">
					￥<span style="font-size: 38rpx;">{{zengsong.coupon_price}}</span>
				</view>
				<view class="tan-z">
					<view class="top-zz">
						{{zengsong.coupon_title}}
					</view>
					<view class="" style="color: #ccc; font-size: 20rpx;">
						赠送人：{{zengsong.nickname}}----{{zengsong.status}}
					</view>
				</view>
			</view>
			<view class="tanchuang-bottom">
				<view class="tanchuang-bottom-tt" @click="querenFn">
					确定
				</view>
			</view>
		</view>
		<view class="mengbic" v-if="zengsong.status=='已使用'&&show==1">

		</view>
		<view class="tanchuang" v-if="zengsong.status=='已使用'&&show==1">
			<view class="tanchuang-top">
				<image src="https://youzhenimg.sdshengyue.cn/attach/2024/01/de227202401181620494758.png" mode=""
					style="width: 100%;height: 194rpx;"></image>
			</view>
			<view class="tanchuang-center">
				<view class="top-jia">
					￥<span style="font-size: 38rpx;">{{zengsong.coupon_price}}</span>
				</view>
				<view class="tan-z">
					<view class="top-zz">
						{{zengsong.coupon_title}}
					</view>
					<view class="" style="color: #ccc; font-size: 20rpx;">
						赠送人：{{zengsong.nickname}}----{{zengsong.status}}
					</view>
				</view>
			</view>
			<view class="tanchuang-bottom">
				<view class="tanchuang-bottom-tt" @click="quxiao">
					取消
				</view>
			</view>
		</view>
		<view class="mengbic" v-if="show1==1">
		
		</view>
		<view class="zhenzeng-t" v-if="show1==1">
			<view class="zhenzeng-t-top">
				您确定要转增吗？
			</view>
			<view class="zhenzeng-t-bottom">
				<view class="zhenzeng-t-bottom-left" @click="quxiaoFn">
					取消
				</view>
				<button class="zhenzeng-t-bottom-right" open-type="share" :data-rid="item">
					确定
				</button>
			</view>
		</view>
		
		<!-- 		<view class="mengbic">
			
		</view>
		<view class="tanchuang">
			<view class="tanchuang-top">
				<image src="https://youzhenimg.sdshengyue.cn/attach/2024/01/de227202401181620494758.png" mode="" style="width: 100%;height: 194rpx;"></image>
			</view>
			<view class="tanchuang-center">
				<view class="top-jia">
						￥<span style="font-size: 38rpx;">1000.00</span>
				</view>
				<view class="tan-z">
					<view class="top-zz">
						{{zengsong.coupon_title}}
					</view>
					<view class="" style="color: #ccc; font-size: 20rpx;">
						赠送人：{{zengsong.nickname}}
					</view>
				</view>
			</view>
				<view class="tanchuang-bottom">
					<view class="tanchuang-bottom-tt" @click="querenFn">
						确定
					</view>
				</view>
			</view> -->





		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<!-- #ifndef MP -->
		<home></home>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		getUserCoupons,
		setCouponReceive1,
		setCouponReceive2,
		xiugaizengs
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import home from '@/components/home';
	import colors from '@/mixins/color.js';
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	export default {
		components: {
			// #ifdef MP
			authorize,
			// #endif
			home
		},
		mixins: [colors],
		data() {
			return {
				imgHost: HTTP_REQUEST_URL,
				couponsList: [],
				loading: false,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				navOn: 1,
				page: 1,
				limit: 15,
				finished: false,
				zengsong: {},
				show: 0,
				userid: null,
				yid: null,
				youhui: {},
				show1:0,
				zhuanzengId:null,
				zhuanzenguid:null
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUseCoupons();
					}
				},
				deep: true
			}
		},
	
		onLoad(options) {
			console.log(options, 'options');
			if (options.userid) {
				this.userid = options.userid;
				this.yid = options.yid;
				}
			if (this.isLogin) {
				this.getYou()
				// this.getUseCoupons();
				uni.showShareMenu({
					//小程序的生菜单中显示分享按钮，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
					menus: ["shareAppMessage", "shareTimeline"] //不设置默认发送给朋友
				})
			} else {
				toLogin();
			}
		},
		onShow() {
			console.log('show');
			let that=this
			that.couponsList=[]
			getUserCoupons(this.navOn, {
				page: 1,
				limit: this.limit
			}).then(res => {
				that.loading = false;
				uni.hideLoading();
				that.couponsList = res.data;
				that.finished = res.data.length < that.limit;
				that.page += 1;
			}).catch(err => {
				that.loading = false;
				uni.showToast({
					title: err,
					icon: 'none'
				});
			});
			if (this.userid != null) {
				this.getYou()
				
			}
		},
		onReachBottom() {
			this.getUseCoupons();
		},
		methods: {
			zhuanzengFn(item){
				this.zhuanzengId=item.id
				this.zhuanzenguid=item.uid
				this.show1=1
			},
			quxiao() {
				this.show = 0
			},
			quxiaoFn(){
				this.show1=0
			},
			onShareAppMessage(res) {
				if (res.from == 'button') {
					// let userid = res.target.dataset.rid.uid
					// let yid = res.target.dataset.rid.id
					let userid = this.zhuanzenguid
					let yid = this.zhuanzengId
					console.log(userid,'userid')
					console.log(yid,'yid')
					xiugaizengs(yid).then(res => {
						// console.log(res,'res');
					})
					getUserCoupons(this.navOn, {
						page: 1,
						limit: this.limit
					}).then(res => {
						that.loading = false;
						uni.hideLoading();
						that.couponsList = res.data;
						that.finished = res.data.length < that.limit;
						that.page += 1;
					}).catch(err => {
						that.loading = false;
						uni.showToast({
							title: err,
							icon: 'none'
						});
					});
					this.show1=0
					return {
						title: '侑珍优惠劵',
						path: "pages/users/user_coupon/index?userid=" + userid + "&yid=" + yid,
						imageUrl: 'https://youzhenimg.sdshengyue.cn/attach/2024/01/a8844202401240914032734.png', //分享图标，路径可以是本地文件路径、代码包文件路径或者网络图片路径.支持PNG及JPG。显示图片长宽比是 5:4
			
					}
				}
			
			},
			querenFn() {
				setCouponReceive2(this.userid, this.yid).then(res => {
					console.log(res, 'res');
					this.youhui = res.data
					// this.getUseCoupons();
					this.userid=null
					this.yid=null
					let that = this;
					uni.showLoading({
						title: that.$t(`正在加载…`)
					});
					getUserCoupons(this.navOn, {
						page: 1,
						limit: this.limit
					}).then(res => {
						that.loading = false;
						uni.hideLoading();
						that.couponsList = res.data;
						that.finished = res.data.length < that.limit;
						that.page += 1;
					}).catch(err => {
						that.loading = false;
						uni.showToast({
							title: err,
							icon: 'none'
						});
					});
					this.show = 0
					if (this.youhui.data == 0) {
						uni.showToast({
							title: "领取成功",
							icon: "none"
						})
					}

					console.log(this.youhui, 'this.youhui');
				})
			},
			getYou() {
				setCouponReceive1(this.userid, this.yid).then(res => {
					console.log(res, 'res');
					this.zengsong = res.data
					this.show = 1
					console.log(this.zengsong, 'this.zengsong');
					
				})
				
			},

			onNav: function(type) {
				this.navOn = type;
				this.couponsList = [];
				this.page = 1;
				this.finished = false;
				this.getUseCoupons();
			},
			useCoupon(item) {
				if (this.navOn == 2) return
				let url = '';
				if (item.category_id == 0 && item.product_id == '') {
					url = '/pages/goods/goods_list/index?title=默认'
				}
				if (item.category_id != 0) {
					url = `/pages/goods/goods_list/index?title=${item.coupon_title}&coupon_category_id=${item.category_id}`
				}
				if (item.product_id != '') {
					let arr = item.product_id.split(',');
					let num = arr.length;
					if (num == 1) {
						url = '/pages/goods_details/index?id=' + item.product_id
					} else {
						url = '/pages/goods/goods_list/index?productId=' + item.product_id + '&title=默认'
					}
				}
				uni.navigateTo({
					url: url
				});
			},
			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.getUseCoupons();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 获取领取优惠券列表
			 */
			getUseCoupons: function() {
				console.log('111');
				let that = this;
				if (that.loading || that.finished) {
					return;
				}
				that.loading = true;
				uni.showLoading({
					title: that.$t(`正在加载…`)
				});
				getUserCoupons(this.navOn, {
					page: this.page,
					limit: this.limit
				}).then(res => {
					that.loading = false;
					uni.hideLoading();
					that.couponsList = that.couponsList.concat(res.data);
					that.finished = res.data.length < that.limit;
					that.page += 1;
				}).catch(err => {
					that.loading = false;
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
			}
		}
	}
</script>

<style>
	.zhenzeng-t{
		width: 594rpx;
		position: fixed;
		height: 200rpx;
		padding: 30rpx;
		top: 50%;
		left: 50%;
		transform: translate(-50%,-50%);
		background-color: #fff;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
	}
	.zhenzeng-t-top{
		text-align: center;
		font-size: 30rpx;
		
	}
	.zhenzeng-t-bottom{
		display: flex;
		justify-content: space-evenly;
		margin-top: 52rpx;
	}
	.zhenzeng-t-bottom-left{
		width: 30%;
		background-color: #E8C389;
		height: 45rpx;
		font-size: 27rpx;
		line-height: 45rpx;
		text-align: center;
		border-radius: 9rpx;
	}
	.zhenzeng-t-bottom-right{
		width: 30%;
		background-color: #E8C389;
		height: 45rpx;
		font-size: 27rpx;
		line-height: 45rpx;
		text-align: center;
		color: #333;
	}
	.mengbic1 {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #404040;
	}
	.mengbic {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #404040;
	}

	.tanchuang-bottom-tt {
		width: 90%;
		margin-left: 5%;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		background-color: #EED6B3;
		border-radius: 16rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #775748;
		margin-top: 60rpx;
	}

	.tan-z {
		font-size: 24rpx;
		padding-left: 10rpx;
		padding-top: 10rpx;
	}

	.top-zz {
		margin-bottom: 36rpx;
	}

	.tanchuang-center {
		display: flex;
		margin: 20rpx 40rpx;
		box-shadow: 0rpx 0rpx 10rpx #c4c4c4;
		color: #333;

	}

	.top-jia {
		width: 200rpx;
		height: 128rpx;
		text-align: center;
		color: #fff;
		font-size: 30rpx;
		line-height: 100rpx;
		background: url("") left top/100% 100% no-repeat;
	}

	.zhuan-img {
		width: 24rpx;
		height: 24rpx;
		vertical-align: middle;
		display: inline-block;
		margin-right: 5rpx;
	}

	.tanchuang {
		position: fixed;
		width: 594rpx;
		height: 548rpx;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		/* padding: 20rpx; */
		background-color: #fff;
		border-radius: 40rpx 40rpx 40rpx 40rpx;
		overflow: hidden;
		/* background-color: ; */
	}

	.tanchuang-top {
		text-align: center;
	}

	.touxiang {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;

	}

	.zhuanzeng {
		color: #E8C389;
		text-align: center;
		margin-left: auto;
		width: 100%;
		font-size: 24rpx;
		height: 45rpx;
		line-height: 45rpx;
		border-top: 1rpx dotted #EEE;
		margin-top: 6rpx;
	}

	.zhuanzeng1 {
		color: #a6a6a6;
		text-align: center;
		margin-left: auto;
		width: 100%;
		font-size: 24rpx;
		height: 45rpx;
		line-height: 45rpx;
		border-top: 1rpx dotted #EEE;
		margin-top: 6rpx;
	}

	.money {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.bg-color {
		background-color: #E8C389 !important;
	}

	.moneyCon {
		background-color: #E8C389 !important;
	}

	.pic-num {
		color: #ffffff;
		font-size: 24rpx;
	}

	.coupon-list .item {}

	.coupon-list .item .text {
		padding: 14rpx 10rpx;
	}

	.coupon-list .item .text .condition {
		display: flex;
	}

	.coupon-list .item .text .condition .name {
		font-size: 24rpx;
		font-weight: 500;
		line-height: 44rpx;
		/* display: flex;
		align-items: center; */
	}

	.coupon-list .item .text .condition .pic {
		width: 30rpx;
		height: 30rpx;
		display: block;
		margin-right: 10rpx;
		display: inline-block;
		vertical-align: middle;
	}

	.condition .line-title {
		/* width: 70rpx; */
		height: 36rpx !important;
		padding: 0 5px;
		line-height: 32rpx;
		text-align: center;
		box-sizing: border-box;
		background: #E8C389 0.3;
		border: 1px solid #E8C389;
		opacity: 1;
		border-radius: 20rpx;
		font-size: 18rpx !important;
		color: #E8C389;
		margin-right: 12rpx;
		text-align: center;
		display: inline-block;
		vertical-align: middle;
	}

	.condition .line-title.bg-color-huic {
		border-color: #BBB !important;
		color: #bbb !important;
		background-color: #F5F5F5 !important;
	}
</style>

<style lang="scss" scoped>
	.navbar {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 90rpx;
		background-color: #FFFFFF;
		z-index: 9;

		.item {
			border-top: 5rpx solid transparent;
			border-bottom: 5rpx solid transparent;
			font-size: 30rpx;
			color: #999999;

			&.on {
				border-bottom-color: #E8C389;
				color: #282828;
			}
		}
	}

	.coupon-list {
		margin-top: 122rpx;
	}

	.noCommodity {
		margin-top: 300rpx;
	}
</style>